home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 February / macformat-034.iso / mac / Shareware City / Developers / simple-sockets-11-c / Simple Sockets 1.1 ƒ / Sample Apps / day_client / Source / day.c next >
Encoding:
C/C++ Source or Header  |  1995-11-27  |  2.1 KB  |  123 lines  |  [TEXT/CWIE]

  1. /*
  2.  * day.c
  3.  *
  4.  * Main file for macintosh day client. to run with my modified dayd daemon.
  5.  *
  6.  * Mike Trent 8/94
  7.  *
  8.  */
  9.  
  10.  
  11.  
  12. /* Main Mac Includes */
  13. #include <Types.h>
  14. #include <Memory.h>
  15. #include <Quickdraw.h>
  16. #include <Fonts.h>
  17. #include <Events.h>
  18. #include <Menus.h>
  19. #include <Windows.h>
  20. #include <TextEdit.h>
  21. #include <Dialogs.h>
  22. #include <OSUtils.h>
  23. #include <ToolUtils.h>
  24. #include <SegLoad.h>
  25.  
  26. /* Additional Mac Includes */
  27. #include <MacTCPCommonTypes.h> //for ip.h
  28.  
  29. /* Ansi Includes */
  30. #include <stdio.h>
  31.  
  32. /* Additional Includes */
  33. #define MAIN
  34. #include "globals.h"
  35. #include "ip.h"
  36.  
  37. void InitToolBox(void);
  38. void get_data(char *str);
  39.  
  40. main()
  41. {
  42.     WindowPtr w;
  43.     Rect     windRect;
  44.     char s[100];
  45.  
  46.     InitToolBox();
  47.     
  48.     SetRect(&windRect, 40,40,300,150);
  49.     if ( (w = NewWindow (nil, &windRect, "\pDate", true, documentProc, (WindowPtr) -1, false, 0)) == nil) {
  50.         SysBeep(3);
  51.         return;
  52.     } 
  53.     SetPort(w);
  54.     
  55.     MoveTo(10,15);
  56.     DrawString("\pDay Client: port 1026");
  57.     
  58.     get_data(s);
  59.     
  60.     MoveTo(10,30);
  61.     DrawString((ConstStr255Param)s);
  62.     
  63.     while (!Button());
  64.     
  65.     CloseWindow(w);
  66. }
  67.     
  68.     
  69. void InitToolBox(void)
  70. {
  71.     InitGraf(&qd.thePort);
  72.     InitFonts();
  73.     InitWindows();
  74.     InitMenus();
  75.     TEInit();
  76.     InitDialogs(nil);
  77.     InitCursor();
  78.     
  79.     if (InitMacTCP() != noErr) {
  80.         SysBeep(3);
  81.         ExitToShell();
  82.     }
  83. }
  84.  
  85. void get_data(char *str)
  86. {
  87.     int s, err, n= 1, tot = 0;
  88.     struct sockaddr_in sa;
  89.     
  90.     s = socket(IPPROTO_TCP);
  91.     if (s < 0) {
  92.         sprintf(str, (const char *)"\psocket() e%d, m%d", gErrno, gMacErrno);
  93.         return;
  94.     }
  95.     
  96.     sa.sin_port = 1026;
  97.     sa.sin_addr = GetHostByName("144.89.40.6");
  98.     //sa.sin_addr = 0x90592866;
  99.     if (sa.sin_addr == 0) {
  100.         sprintf(str, (const char *)"\pGetHostByName() e%d, m%d", gErrno, gMacErrno);
  101.         return;
  102.     }    
  103.     
  104.     if ((err = connect(s,&sa)) != noErr) {
  105.         sprintf(str, (const char *)"\pconnect error");
  106.         return;
  107.     }
  108.         
  109.     while ((n=mac_read(s, str, 100)) > 0) {
  110.         str[n] = '\0';
  111.         tot += n;  //keep a running total of length of bytes received.
  112.     }    
  113.  
  114.     for (n=tot-1; n >=0; n--) {
  115.         str[n+1] = str[n];
  116.     }
  117.     str[0] = tot-1;
  118.     
  119.     if (mac_close(s)) {
  120.         //sprintf(str, (const char *)"\pclose error() e%d, m%d", gErrno, gMacErrno);
  121.         return;
  122.     }
  123. }